package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;

public class ChangePhoneNumberActivity extends BaseActivity implements View.OnClickListener {

    private static final int STATE_OLD = 0;
    private static final int STATE_NEW = 1;

    private EditText phoneNumber;
    private FrameLayout first;
    private FrameLayout second;

    private int state = 0;
    private String number;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone_number);

        initViews();
        initDatas();
    }

    private void initDatas() {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        number = extras.getString("number");
        phoneNumber.setText(number);
        phoneNumber.setSelection(number.length());
    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("更换手机号");

        phoneNumber = ((EditText) findViewById(R.id.mine_setting_change_phonenumber));
        findViewById(R.id.mine_setting_change_button).setOnClickListener(this);
        findViewById(R.id.change_phone_number_enter_getyanzhengma).setOnClickListener(this);

        first = ((FrameLayout) findViewById(R.id.change_phone_number_first));
        second = ((FrameLayout) findViewById(R.id.change_phone_number_second));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.head_back:
                if(state == STATE_NEW){
                    second.setVisibility(View.GONE);
                    first.setVisibility(View.VISIBLE);
                    state = STATE_OLD;
                }else
                finish();
                break;
            case R.id.mine_setting_change_button:
                state = STATE_NEW;
                if (!judgeNumber()) {
                    break;
                }
                first.setVisibility(View.GONE);
                second.setVisibility(View.VISIBLE);
                break;
            case R.id.change_phone_number_enter_getyanzhengma:
                Toast.makeText(ChangePhoneNumberActivity.this,"发送成功",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private boolean judgeNumber() {
        boolean flag = true;
        String number = phoneNumber.getText().toString().trim();
        if(TextUtils.isEmpty(number)){
            Toast.makeText(ChangePhoneNumberActivity.this,"手机号码不能为空",Toast.LENGTH_SHORT).show();
            flag = false;
            return flag;
        }else if(number.length() != 11){
            Toast.makeText(ChangePhoneNumberActivity.this,"请输入正确的手机号码",Toast.LENGTH_SHORT).show();
            flag = false;
            return flag;
        }

        return flag;
    }
}
